<?php

namespace app\admin\controller\hd\forum;

use app\common\controller\Backend;
use fast\Tree;

/**
 * 论坛版块
 *
 * @icon fa fa-circle-o
 */
class Type extends Backend
{

    /**
     * Type模型对象
     * @var \app\admin\model\hd\forum\Type
     */
    protected $model = null;
    protected $typelist = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\hd\forum\Type;
        $maxLevel = $this->model->max('level');
        $this->view->assign('maxLevel', $maxLevel);
        $this->assignconfig(['maxLevel' => $maxLevel]);
    }



    /**
     * 查看(表1)
     */
    public function table1()
    {
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where('level', 1)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch('index');
    }
    /**
     * 查看(表2)
     */
    public function table2()
    {
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where('level', 2)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch('index');
    }

    /**
     * 查看(表3)
     */
    public function table3()
    {
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where('level', 3)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch('index');
    }

    /**
     * 添加
     */
    public function add()
    {
        $this->typedata();
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $params['create_time'] = time();
            if ($params['level'] == 1) {
                $params['superior_id'] = 0;
            }
            $add = $this->model->insert($params);
            if ($add) {
                $this->success('添加成功');
            }
            $this->error('操作失败');
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $this->typedata($ids);
        $row = $this->model->get(['id' => $ids]);
        if ($this->request->isPost()) {
            $edit = $this->model->get($ids);
            $params = $this->request->post("row/a");
            $params['update_time'] = time();
            if ($params['level'] == 1) {
                $params['superior_id'] = 0;
            }
            if ($edit->allowField(true)->save($params)) {
                $this->success(__('修改成功'));
            }
            $this->error(__('操作失败！'));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 获取上级2维数组 (内部方法)
     */
    private function typedata($id = 0)
    {
        $typeList = $this->model->where('id', 'neq', $id)->order('id ASC')->select();
        foreach ($typeList as $k => &$v) {
            $v['title'] = __($v['name']);
            $v['pid'] = $v['superior_id'];
        }
        unset($v);
        Tree::instance()->init($typeList)->icon = ['&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;'];
        $this->typelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
        $typedata = [0 => __('None')];
        foreach ($this->typelist as $k => &$v) {
            $typedata[$v['id']] = $v['title'];
        }
        unset($v);
        $this->view->assign('typedata', $typedata);
    }
}
